package com.flyqiu.flow.api.service.base;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyqiu.flow.common.enums.VarScopeType;
import com.flyqiu.common.tools.StringTools;
import com.flyqiu.flow.model.entity.base.Variable;
import com.flyqiu.flow.model.entity.hi.ProcHiVariable;
import com.flyqiu.flow.model.entity.ru.ProcRuVariable;

import java.util.List;

public interface FlowVariableService {
    /**
     * 添加流程变量
     *
     * @param procInstId
     * @param varMap
     */
    default void addVariables(String procInstId, JSONObject varMap) {
        addVariables(procInstId, null, varMap);
    }

    /**
     * 添加流程活动变量
     *
     * @param procInstId
     * @param actId
     * @param varMap
     */
    default void addVariables(String procInstId, String actId, JSONObject varMap) {
        addVariables(procInstId, actId, null, varMap);
    }

    void addVariables(String procInstId, String actId, String actInstId, JSONObject varMap);

    default void moveToHistory(String procInstId) {
        moveToHistory(procInstId, null);
    }

    void moveToHistory(String procInstId, String actId);

    default List<ProcRuVariable> listRuVariable(String procInstId) {
        return listRuVariable(procInstId, null);
    }

    default List<ProcRuVariable> listRuVariable(String procInstId, String actId) {
        VarScopeType varScopeType = VarScopeType.PROCESS;
        if (StringTools.isNotBlank(actId)) {
            varScopeType = VarScopeType.ACT;
        }
        LambdaQueryWrapper<ProcRuVariable> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Variable::getProcInstId, procInstId);
        lambdaQueryWrapper.eq(Variable::getVarScope, varScopeType.getValue());
        lambdaQueryWrapper.eq(varScopeType == VarScopeType.ACT, Variable::getActId, actId);
        return listRuVariable(lambdaQueryWrapper);
    }

    List<ProcRuVariable> listRuVariable(LambdaQueryWrapper<ProcRuVariable> lambdaQueryWrapper);

    default List<ProcHiVariable> listHiVariable(String procInstId) {
        return listHiVariable(procInstId, null);
    }

    default List<ProcHiVariable> listHiVariable(String procInstId, String actId) {
        VarScopeType varScopeType = VarScopeType.PROCESS;
        if (StringTools.isNotBlank(actId)) {
            varScopeType = VarScopeType.ACT;
        }
        LambdaQueryWrapper<ProcHiVariable> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Variable::getProcInstId, procInstId);
        lambdaQueryWrapper.eq(Variable::getVarScope, varScopeType.getValue());
        lambdaQueryWrapper.eq(varScopeType == VarScopeType.ACT, Variable::getActId, actId);
        return listHiVariable(lambdaQueryWrapper);
    }

    List<ProcHiVariable> listHiVariable(LambdaQueryWrapper<ProcHiVariable> lambdaQueryWrapper);

    default void deleteRuVariable(String procInstId) {
        deleteRuVariable(procInstId, null);
    }

    void deleteRuVariable(String procInstId, String actId);

    default void deleteHiVariable(String procInstId) {
        deleteHiVariable(procInstId, null);
    }

    void deleteHiVariable(String procInstId, String actId);
}
